package de.stefanocke.roo.wicket.templates

import de.stefanocke.roo.wicket.Stereotypes

class WicketApplicationTemplate extends JavaHtmlTemplate{
	
	override String javaClassNameWhenNew(){
		"WicketApplication"
	}
	
	override String stereotype(){
		Stereotypes::APPLICATION
	}
		
	override buildJavaContents(){
		import_("org.apache.wicket.ConverterLocator")
		import_("org.apache.wicket.IConverterLocator")
		import_("org.apache.wicket.Page")
		import_("org.apache.wicket.protocol.http.WebApplication")
		import_("org.apache.wicket.spring.injection.annot.SpringComponentInjector")
		import_("org.apache.wicket.util.convert.converter.CalendarConverter")
		val HomePage = import_(typeof(HomePageTemplate).generatedJavaType)
		'''
		/**
		 * Application object for the Wicket web application. 
		 */
		«stereotypeAnnotation»
		public class «simpleTypeName» extends WebApplication
		{    
			/**
			 * @see wicket.Application#getHomePage()
			 */
			@Override
			public Class<? extends Page> getHomePage()
			{
				return «HomePage».class;
			}
			
			@Override
			protected void init() {
				super.init();
				getComponentInstantiationListeners().add(new SpringComponentInjector(this));
			}
			
			@Override
			protected IConverterLocator newConverterLocator() {
				ConverterLocator converterLocator = new ConverterLocator();
				// necessary, since CalendarConverter is registered only for Calendar, but not for its subclasses.
				converterLocator.set(GregorianCalendar.class, new CalendarConverter());
				return converterLocator;
			}
		}
		'''
	}
}